package com.liu.netty.service;

import com.liu.netty.netty.ClientManager;
import com.liu.netty.netty.ProtocolContant;
import com.liu.netty.pojo.Message;
import com.liu.netty.util.DateTimeUtil;
import com.liu.netty.util.ImageUtils;

import com.liu.netty.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CollectService {

    @Autowired
    private SenorService senorServer;

    @Autowired
    private RunInfoService runInfoService;

    @Autowired
    private SelfCheckService selfCheckService;

    @Autowired
    private ErrorInfoService errorInfoService;

    /**
     *  消息处理类
     * @param message
     * @param msg
     * @return
     */
    public String detailMessageAndRespose(Message message, String msg) {
        if(!StringUtils.isEmpty(message.getCommandStyle())){
            msg = detailMsgCommandStyleEquleOne(message, msg);
        }else {

            Message mss =ClientManager.taskMap.get( message.getTaskID());
            if(mss.getCommandStyle().equalsIgnoreCase("2")){
               message.setObjectAddress(mss.getObjectAddress());
               message.setDataID(mss.getDataID());


              msg=  detailMsgCommandStyleEqTwo(message,msg);

            }else if(mss.getCommandStyle().equalsIgnoreCase("3")){

            }else {
                System.out.println(message);
            }





        }

        return msg;
    }

    private String detailMsgCommandStyleEqThree(Message message, String msg) {
        switch (Integer.parseInt(message.getDataID())){
            case 1://软件升级
                msg="";
                break;

        }
        return msg;
    }

    private String detailMsgCommandStyleEqTwo(Message message, String msg) {
        switch (Integer.parseInt(message.getDataID())){
            case 1://设备复位
                 msg="";
                break;
            case 2://设置参数
                msg="";
                break;
            case 3://读取参数
                System.out.println("参数为："+message);

                break;
            case 4://读取软件版本号
                break;
            case 5://设置时间
                break;
            case 6://读取时钟
                break;
            case 7://读取自检日志
                break;
            case 8://读取传感器数据
                break;
            case 9://控制摄像头拍照
                break;

        }
        return msg;
    }

    /**
     * 处理消息指令为 1的类型
     * @param message
     * @param msg
     * @return
     */
    private String detailMsgCommandStyleEquleOne(Message message, String msg) {
        switch (Integer.parseInt(message.getDataID())){
            case 1:// 登录信息

                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;
            case 2://心跳
                String time = DateTimeUtil.getTodayChar19();
                msg ="###<TaskID=" + message.getTaskID() + ";TaskContent{ResultFlg=0;Content<DateTime=" + time + ">}>###";
                break;
            case 3:// 上报图片信息
                long PicTime = System.currentTimeMillis();
                String pathName="d:/image/"+message.getObjectAddress()+"_" +ProtocolContant.getCamWay(message.getCamWay())+"_"+ message.getImageIdx() +"_"+PicTime+".jpg";
                System.out.println("pathName"+pathName);
                boolean flag = ImageUtils.base64DecodeImage(message.getImage(),"d:/image",pathName,false);
                System.out.println("照片结果："+flag);
                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;
            case 4:// 设备运行信息
                runInfoService.addRunInfoFromClient(message);
                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;
            case 5:// 上报错误信息
               errorInfoService.addErrorInfoDaoFromClient(message);
                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;
            case 6:// 上报自检日志

                selfCheckService.addSelfCheckFromClient(message);
                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;
            case 7:// 上报传感器数据
                senorServer.upSenorFromClient(message);
                msg ="###<TaskID="+message.getTaskID()+";TaskContent{ResultFlg=0;Content<>}>###";
                break;

        }
        return msg;
    }


    public void sendCommand(int dataId,String address,String resetFlag,String camway) {
        String taskId = ClientManager.getINSTANCE().getTaskId();
        String command = ProtocolContant.getCommandFromType(3,taskId,address,null,null);
        Message message = MessageUtil.messageDecode(command);
        ClientManager.taskMap.put(taskId,message);
        ClientManager.getChannelId(address).writeAndFlush(command);
    }

}
